package com.gzx.plugin.tjzy.modular.call.ht.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zjt
 * @description
 * @date 2025/3/10 14:15
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "callee")
public class CallInProperties {
    private Map<String, CallItem> items;

    @Data
    public static class CallItem {
        private String tenantId;
        private List<ItemDetail> item;
    }

    @Data
    public static class ItemDetail {
        private String deptId;
        private List<String> phone;
    }

    public Map<String, Map<String, String>> getMapByCallee() {
        return items.values().stream()
                .flatMap(callItem -> callItem.getItem().stream()
                        .flatMap(itemDetail -> itemDetail.getPhone().stream()
                                .collect(Collectors.toMap(
                                        phone -> phone,
                                        phone -> Map.of("deptId", itemDetail.getDeptId(), "tenantId", callItem.getTenantId())
                                )).entrySet().stream()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    public Map<String, String> getTenantIdAndDeptIdByCallee(String phone) {
        return items.entrySet().stream()
                .flatMap(entry -> entry.getValue().getItem().stream()
                        .filter(itemDetail -> itemDetail.getPhone().contains(phone))
                        .map(itemDetail -> Map.of(
                                "tenantId", entry.getValue().getTenantId(),
                                "deptId", itemDetail.getDeptId()
                        )))
                .findFirst()
                .orElse(Map.of());
    }
}